<?php

/*
    小红书解析
*/
//*****执行代码********
//$url = $_GET['url'];

//
 $listUrl='https://www.xiaohongshu.com/user/profile/61d3b749000000001000d5a5';


   function curlGet($url, $file)
   {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_POST, 0);
      curl_setopt($ch,CURLOPT_URL,$url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      $file_content = curl_exec($ch);
      curl_close($ch);
      $downloaded_file = fopen($file, 'w');
      fwrite($downloaded_file, $file_content);
      fclose($downloaded_file);
   }

   function openGet($url, $file)
   {
      $in = fopen($url, "rb");
      $out = fopen($file, "wb");
      while ($chunk = fread($in,8192))
      {
         fwrite($out, $chunk, 8192);
      }
      fclose($in);
      fclose($out);
   }


function curl($url, $header, $method) {
    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $url);
    if (!empty($header)) {
        curl_setopt($curl, CURLOPT_HEADER, $header);
    }

    if ($method == "OPTIONS") {
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    }

    //curl_setopt ($curl,CURLOPT_REFERER,'https://www.xiaohongshu.com/explore/6484939d000000001300530c');
    curl_setopt ($curl,CURLOPT_REFERER,'https://www.xiaohongshu.com/');
    curl_setopt($curl, CURLOPT_COOKIE, 'a1=185baf34f93jq55xybg20miiqk0rtvn241egsrun630000212414; webId=656e9028014019ab278788a8e4f58ebd; gid=yY2D0iq2Yf4DyY2D0iq4iyF7jqVA22CvDkJ8dIK633AT8kq8F9hIJM888JyJ4y482JD02SD4; gid.sign=91S8qIEgaxu5NwiXHcmR7xA5rnk=; xhsTrackerId=9a2debf2-6dcf-4a0d-9d07-651f2cb22dd7; xhsTrackerId.sig=PqzplzSoTaXL-Wod5vJ0_OQ4RoZ_RlGovYA0xy_zqZc; customerClientId=686367408832523; x-user-id-gaia.xiaohongshu.com=648458c27500000000000002; x-user-id-ark.xiaohongshu.com=648458c27500000000000002; timestamp2=1688216379853af0e67aba5efbe67efd1fc5c3355858d9d52ab5fcef6ac458c; timestamp2.sig=hhnYsqEXoYdqMpdzOP0639RvUVGwXeWkoinxMEji4QI; xsecappid=xhs-pc-web; webBuild=2.18.4; web_session=040069b5f1cc386e10c03f30fd364b800f2e32; acw_tc=4d2acc59a41c2d7100199d701fec64353e56e074719b5892eff890ad52483e89; websectiga=a9bdcaed0af874f3a1431e94fbea410e8f738542fbb02df1e8e30c29ef3d91ac; sec_poison_id=e4a33315-70b6-4c4f-acbe-3b7ee6849e8a');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_USERAGENT,'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36');


    //curl_setopt($curl, CURLOPT_X_B3_Traceid, "5610ecf550c2f6b0");
    //curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    //curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    $data = curl_exec($curl);

    curl_close($curl);
    return $data;
}

function getCursor($scripts) {
    foreach ($scripts[1] as $script) {
        $pos = strpos($script, "noteQueries");

        if ($pos <= 0) {
            continue;
        }
        $script = str_replace('\u002F', '/', $script);
        $script = str_replace('undefined', '""', $script);

        //var_dump($pos, $script);
        $s = str_replace( "window.__INITIAL_STATE__=", "", $script);
        $arr = json_decode($s, true);
        return $arr['user']['noteQueries'][0];
    }
}

$data = curl($listUrl, [], 'GET');
//echo $data;
//$pattern = "/<script>(.*?)<\/script>/is"; // 匹配 script 标记的正则表达式
//preg_match_all($pattern, $data, $scripts); // 执行匹配


preg_match_all('/"id":"(.*)"/U', $data, $matches);
var_dump($matches[1][0]);

//var_dump($scripts[1]); die;
//var_dump($matches); die;

$detailUrlPre = "https://www.xiaohongshu.com/explore/";
foreach($matches[1] as $id) {
    $url = $detailUrlPre.$id;
    $d = curl($url, [], 'GET');
    //echo $d;

    preg_match_all('/"masterUrl":"(.*)"/U', $d, $mp4Arr);

    $mp4Url = $mp4Arr[1][0];
    $mp4 = str_replace('\u002F', '/', $mp4Url);
    var_dump($mp4);

    $fileName = basename($mp4);
    if (file_exists($fileName)) {
        continue;
    } else {
        $rs = exec("wget {$mp4}");
    }
}


// 找到翻页的cursor
//$next = getCursor($scripts);
//$cursor = $next['cursor'];
//$userId = $next['userId'];
//$num = $next['num'];
//var_dump($cursor);
//
//$nextUrl =  "https://edith.xiaohongshu.com/api/sns/web/v1/user_posted?num=30&cursor={$cursor}&user_id={$userId}";
//echo $nextUrl;
//$header = [
//    'X-B3-Traceid' => '5610ecf550c2f6b0',
//    'X-S' => 'XYW_eyJzaWduU3ZuIjoiNTEiLCJzaWduVHlwZSI6IngxIiwiYXBwSWQiOiJ4aHMtcGMtd2ViIiwic2lnblZlcnNpb24iOiIxIiwicGF5bG9hZCI6ImVlYjY4MjQ0NjVkNjBjMGJmMmFlNGJjNTI3MGIwM2VmMDA1ZjUyODI1N2FiNDM4OWE1NzI4MzdlZjJmYmM5ZDg1ZDA0ZGY1NzZhNTk0YjdmMDVmYjUwYmJmNDU1YmM2MWM5ZTNiZmRhMWZhYTFlYjkwZDc0YWEzMWI1NGM3MmNkMGQ3NGFhMzFiNTRjNzJjZGFjNDg5YjlkYThjZTVlNDhmNGFmYjlhY2ZjM2VhMjZmZTBiMjY2YTZiNGNjM2NiNWQ1MmMxZmIzNTY0M2RkMTMzYzMwNTZjYzdhYTJmNGE3M2U2YzRkZDUyMTFkNTc2ODEyYmZjYzBhZjBmNjkxOWY3ZTBlMDlkNTQ0NzZiODQ0MTZmYjQ1Y2FjZTk2Y2FmNDc2MzBiYzJlYWFkNDBiNDYxYjgzYzQwMGNjODdiYjc5MWU1N2ViYzJiYWFlMDJkMjRhNWMwNmE4MTVmZjhhNDc0Yjg5ODM4NWI5NzhhN2QwN2M3Mzg3NWVlYjQyMDNlZiJ9',
//    'X-S-Common' => '2UQAPsHCPUIjqArjwjHjNsQhPsHCH0rjNsQhPa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SzQ2bPAGdb7zgQB8nph8emSy9E0cgk+zSS1qgzianYt8p+f/LzN4gzaa/+NqMS6qS4HLozoqfQnPbZEp98QyaRSp9P98pSl4oSzcgmca/P78nTTL0bz/sVManD9q9z1J7+xJMcM2gbFnobl4MSUcdb6agW3tF4ryaRApdz3agWIq7YM47HFqgzkanTU4FSkN7+3G9PAaL+P8DDA/9LI4gzVP0mrnd+P+nprLFkSyS87PrSk8nphpd4PtMmFJ7Ql4BYcnLRSy9Mg+rSht9SQyoQa2S878FTc4bSQPMbcJFlN8/8l4BYQ2sRA+S8FJFSk/nRQyLL68FH98p+Dae+U8d8AydpFa7Qy89pfG7HE898N8pS0Lo+Q2epSzopFNFSb+fpDLozBGSSUyrh7/BEQ2epAP7bFLfE0+9pn8Dq3anT04FSkaocFPBQ+ag8iqgz/wB4QynSfqb87cLSeab8tJA+SL7mS8nTc4b8Q2e+SPBkHOaHVHdWEH0iTP/PM+Ar9PAPl+sIj2erIH0ilwoF=',
//    'X-T' => '1688119556068',
//    'Content-Type' => 'application/json',
//];
//
//$data = curl($nextUrl, $header, "OPTIONS");
//var_dump($data);
// 组装翻页url
die;

// 翻页
?>
